<?php
class ImgModel extends MY_Model{
    public $table = 't_img';
    public function __construct(){
        parent::__construct();
    }

    /**
     * 取图库列表
     * name: getImgList
     * @param string $type 图片类型，1帖子，2头像
     * @param string $tag_id 标签id
     * @param string $is_related 是否有关联过内容
     * @Author:taiyang
     */
    public function getImgList($type='',$tag_id='',$is_related='',$is_hege='',$page=1,$pagesize=10){
        $sql="select ti.*,tit.title from t_img ti 
                left join t_img_to_tag titt on ti.id=titt.img_id
                left join t_img_tag tit on tit.id=titt.tag_id  
                where 1 ";
        if(!empty($is_related)){
            $sql.=" and ti.related_id=0";
        }
        //查出一级id

        if(!empty($tag_id)){
            $sql.=" and titt.tag_id in(select id from t_img_tag where parent_id='".$tag_id."')";
        }
        if(!empty($type)){
            $sql.=" and ti.type=".$type;
        }
        $sql.=$this->is_hege($is_hege);
        $sql.=" and ti.status=1 ";
        $index = $page - 1 < 0 ? 0 : ($page - 1) * $pagesize;
        $sql.=" ORDER BY add_time desc limit ".$index.",".$pagesize."";
        return $this->query_sql_list($sql);

    }

    /**
     * 取图库列表总数
     * name: getImgListNum
     * @param string $type
     * @param string $tag_id
     * @param string $is_related
     * @return array
     * @throws Exception
     * @Author:taiyang
     */
    public function getImgListNum($type='',$tag_id='',$is_related='',$is_hege=''){
        $sql="select count(*)as nums from t_img ti 
                left join t_img_to_tag titt on ti.id=titt.img_id
                where 1 ";
        if(!empty($is_related)){
            $sql.=" and ti.related_id=0";
        }
        if(!empty($tag_id)){
            $sql.=" and titt.tag_id in(select id from t_img_tag where parent_id='".$tag_id."')";
        }
        if(!empty($type)){
            $sql.=" and ti.type=".$type;
        }
        $sql.=$this->is_hege($is_hege);
        return $this->query_sql_count($sql);
    }

    /**
     * 生成检测是否合格
     * name: is_hege
     * @param string $num
     * @return string
     * @Author:taiyang
     */
    public function is_hege($num=''){
        $sql='';
        if(!empty($num)){
            if($num == 1){//合格
                $sql.=" and ti.w > 720 and ti.h>720 and ti.size > 20480 ";
            }else{
                $sql.=" and (ti.w < 720 or ti.h<720 or ti.size < 20480) ";
            }
        }
        return $sql;
    }

}